package cn.fjy.android.phone.view;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import android.app.ListActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import cn.fjy.android.res.Constant;
import cn.fjy.android.utils.MusicPlayer;

/**
 * @author GrayF 2011-8-17
 * @version 1.0
 */
public class RecordFileManager extends ListActivity {

	private final static String tag = "RecordFileManager";

	private ArrayList<Map<String, Object>> mData;

	private MusicPlayer mPlayer;

	// 列表中的文件
	private File files[];

	private ListView mListView;

	// 当前选中的文件ID
	private int selectFileId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		init();
		if(mListView!=null){
			// 长按菜单
			mListView
					.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
	
						@Override
						public void onCreateContextMenu(ContextMenu menu,
								View arg1, ContextMenuInfo arg2) {
							menu.setHeaderTitle("管理文件");
							menu.add(0, 0, 0, "播放");
							menu.add(1, 1, 1, "发送");
							menu.add(2, 2, 2, "删除");
						}
					});
	
			// 列表元素长按事件
			mListView.setOnItemLongClickListener(new OnItemLongClickListener() {
	
				@Override
				public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
						int index, long arg3) {
					selectFileId = index;
					return false;
				}
			});
	
			// 列表元素单击停止播放
			mListView.setOnItemClickListener(new OnItemClickListener() {
	
				@Override
				public void onItemClick(AdapterView<?> adapterView, View view,
						int index, long arg3) {
					if (index == selectFileId && mPlayer != null) {
						mPlayer.stop();
						adapterView.getChildAt(selectFileId).setBackgroundDrawable(
								mListView.getBackground());
					}
				}
			});
		}
		super.onCreate(savedInstanceState);
	}

	// 菜单点击事件
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 0:
			showMessage(files[selectFileId].getAbsolutePath());
			mPlayer = new MusicPlayer();
			mPlayer.play(files[selectFileId].getAbsolutePath());
			mListView.getChildAt(selectFileId).setBackgroundColor(Color.GREEN);
			break;

		case 1:
			break;
		case 2:
			try {
				files[selectFileId].delete();
				init();
				showMessage("删除成功");
			} catch (Exception e) {
				showMessage("删除失败");
			}
			break;
		default:
			break;
		}
		return super.onContextItemSelected(item);
	}

	@Override
	protected void onPause() {
		files = null;
		mData = null;
		super.onPause();
	}

	// 初始化列表
	private void init() {
		File file = new File(Constant.RECORD_FILEPATH);
		files = file.listFiles();
		if(files == null){
			return ;
		}
		Arrays.sort(files,new CompratorByLastModified());
		mData = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < files.length; i++) {
			Map<String, Object> item = new HashMap<String, Object>();
			item.put("title", files[i].getName());
			item.put("text", long2DateString(files[i].lastModified()));
			mData.add(item);
		}
		mListView = getListView();
		SimpleAdapter mAdapter = new SimpleAdapter(this, mData,
				android.R.layout.simple_list_item_2, new String[] { "title",
						"text" }, new int[] { android.R.id.text1,
						android.R.id.text2 });
		setListAdapter(mAdapter);
	}

	// 显示提示信息
	private void showMessage(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}

	// long类型时间转换为字符串形式
	private String long2DateString(long time) {
		String result = "";
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			result = sf.format(new Date(time));
		} catch (Exception e) {
			Log.e(tag, "日期转换错误：" + e.getMessage());
		}
		return result;
	}

}

class CompratorByLastModified implements Comparator<File> {
	public int compare(File f1, File f2) {
		long diff = f1.lastModified() - f2.lastModified();
		if (diff > 0)
			return -1;
		else if (diff == 0)
			return 0;
		else
			return 1;
	}

	public boolean equals(Object obj) {
		return true;
	}
}
